package com.it.data_recursion.pascal_triangle;

/**
 * @Author: Coke
 * @DateTime: 2023/06/04/18:07
 * @注释: 杨辉三角
 **/
public class PascalTriangle {
    public static int element (int i, int j) {
        if (j == 0 || i == j) {
            return 1;
        }
        return element(i - 1, j - 1) + element(i - 1, j);
    }
    
    public static void print (int n) {
        for (int i = 0; i < n; i++) {
            printSpace(n, i);
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-4d", element(i, j));
            }
            System.out.println();
        }
    }
    
    /**
     * @return void
     * @作者: Coke
     * @注释: 打印空格 保证杨辉三角是对齐的
     * @DateTime: 2023/6/4 18:20
     */
    private static void printSpace (int n, int i) {
        int num = (n - i - 1) * 2; // 空格的数量
        for (int j = 0; j < num; j++) {
            System.out.print(" ");
        }
    }
}
